<!--<a href="#/layui/memberLev" class="layui-btn layui-btn-xs layui-btn-primary" style="margin: 20px 0 0 100px;">
  <i class="layui-icon">&#xe65c;</i>
</a>-->
<form action="" class="layui-form">
	
    <div class="layui-col-xs12">
      <!--<h2 class="title">-->
      	<!--<a href="#/layui/memberLev" class="layui-btn layui-btn-xs layui-btn-primary" style="margin: -0px 0 0 0;border: none;background-color: rgba(0,0,0,0);">
				  <i class="layui-icon">&#xe65c;</i>
				</a>-->
				<div class="layui-card">
				<div class="layui-card-header">
					<h2>添加等级</h2>
				</div>
      	
      	<!--</h2>-->
      	<div class="layui-card-body">
      		<div class="layui-card">
     <form action="" class="layui-form">
     	
     	<div class="layui-form-item">
  			<div class="layui-form-label">分类</div>
  			<div class="layui-input-block">
  				<div class="select25">
	  				<select name="classify" class="classify" >
						</select> 
					</div>
  			</div>
  		</div>
  		<!--<div class="layui-form-item">
  			<div class="layui-form-label">数量<span class="red">*</span></div>
  			<div class="layui-input-block">
  				<input type="text" name="num"  autocomplete="off" lay-verify="number" class="layui-input num">
  			</div>
  		</div>-->
  		<div class="layui-form-item">
  			<div class="layui-form-label">会员等级名称<span class="red">*</span></div>
  			<div class="layui-input-block">
  				<input type="text" name="level"  autocomplete="off" lay-verify="required"  class="layui-input level input33">
  			</div>
  		</div>
  		<div class="layui-form-item">
  			<div class="layui-form-label">积分满足点<span class="red">*</span></div>
  			<div class="layui-input-block">
  				<input type="text" name="title" autocomplete="off" lay-verify="number"  class="layui-input score input33">
  			</div>
  		</div>
  		<div class="layui-form-item">
  			<div class="layui-form-label">是否默认等级</div>
  			<div class="layui-input-block">
  				<input type="radio" name="isDefault" class="isDefault isDe" value="0" title="否">
							<input type="radio" name="isDefault" class="isDefault notDe" value="1" title="是">
  			</div>
  		</div>
  		<div class="layui-form-item">
  			<div class="layui-form-label">等级图标</div>
  			<div class="layui-input-block">
  				<label for="levPicSub" class="levLogo">
			      			<img src="img/addImg.png" class="levImg" style="width: 100px;height: 100px;"/>
			      	</label>
									<input type="file" id="levPicSub" onchange="uploadPhoto2(this)" style="display: none;" />
  			</div>
  		</div>
  		<!--<div class="layui-form-item">
  			<div class="layui-form-label"></div>
  			<div class="layui-input-block">
  				<a href="#/layui/memberLev" class="addLevBtn layui-btn layui-btn-primary">返回</a>
  				<button class="addLevBtn layui-btn confirm" lay-submit lay-filter="queding" type="button">确定</button>
	      	
  			</div>
  		</div>-->
  		
  		
     	
      	<!--<table class="layui-table" lay-size="lg" style="table-layout:fixed">
		      <colgroup>
					    <col width="62">
					    <col width="500">
					  </colgroup>
			    <tr>
			      <td>分类：</td>
			      <td>
			      	<div class="select25">
						    <select name="classify" class="classify" >
								</select> 
							</div>
			      </td>
			    </tr>
			    <tr>
			      <td>数量：<span class="red">*</span></td>
			      <td><input type="text" name="num"  autocomplete="off" lay-verify="number" class="layui-input num input33"></td>
			    </tr>
			    <tr>
			      <td>会员等级名称：<span class="red">*</span></td>
			      <td><input type="text" name="level"  autocomplete="off" lay-verify="required"  class="layui-input level input33"></td>
			    </tr>
			    <tr>
			      <td>积分满足点：<span class="red">*</span></td>
			      <td><input type="text" name="title" autocomplete="off" lay-verify="number"  class="layui-input score input33"></td>
			    </tr>
			    <tr>
			      <td>是否默认等级：</td>
			      <td>
			      	<input type="radio" name="isDefault" class="isDefault isDe" value="0" title="否">
							<input type="radio" name="isDefault" class="isDefault notDe" value="1" title="是">
			      </td>
			    </tr>
			    <tr>
			      <td>等级图标：</td>
			      <td>
			      	
			      		<label for="levPicSub" class="levLogo">
			      			<img src="img/addImg.png" class="levImg" style="width: 100px;height: 100px;"/>
			      		</label>
									<input type="file" id="levPicSub" onchange="uploadPhoto2(this)" style="display: none;" />
			      		
			      	
			      	
			      </td>
			    </tr>
		      	
	      </table>
	      
	      <div class="addLevBtns">
	      	<button class="addLevBtn layui-btn layui-btn-primary confirm" lay-submit lay-filter="queding" type="button">确定</button>
	      	<a href="#/layui/memberLev" class="addLevBtn layui-btn">返回</a>
	      </div>-->
	      <div class="addLevBtns">
	      	<a href="#/layui/memberLev" class="addLevBtn layui-btn layui-btn-primary">返回</a>
	      	<button class="addLevBtn layui-btn  confirm" lay-submit lay-filter="queding" type="button">确定</button>
	      	
	      </div>
		
	      	
      	</form>
      </div>
      
      
      
      
      
      
      
    </div>
	</div>
	</div>
</form>

<!--<script type="text/javascript">
	let num = document.querySelector(".num");
	let level = document.querySelector(".level");
	let score = document.querySelector(".score");
	let level_ta = document.querySelector(".level_ta");
	let confirm = document.querySelector(".confirm");
	
	
	layui.use(['jquery', 'mockjs', 'table', 'sidebar', 'form','route'], function () {
		var $ = layui.jquery,
      layer = layui.layer,
      table = layui.table,
      sidebar = layui.sidebar,
      route = layui.route,
      params = route.params(),
      form = layui.form;
      
      
    })
	
	
</script>-->

<style scoped>
	.levImg{
		margin-right: 30px;
	}
  	.title{
  		margin-left: 0px;
  		margin-top: 30px;
  	}
  	.memLevBtn{
  		margin-left: 15px;
  		margin-top: 10px;
  		
  	}
  	.memLevBtn button{
  		border-radius: 5px;
  	}
  	.addLevBtns{
  		margin-top: 20px;
  		display: flex;
  		align-items: center;
  		justify-content:  center;
  	}
  	.addLevBtn{
  		width: 200px;
  		border-radius: 5px;
  	}
  	.red{
  		color: red;
  	}
  	
  </style>
  
<script>
	let classify = document.querySelector(".classify");
//	let num = document.querySelector(".num");
	let level = document.querySelector(".level");
	let score = document.querySelector(".score");
	let isDe = document.querySelector(".isDe");
	let notDe = document.querySelector(".notDe");
	let pic = document.querySelector(".pic");
	let elseText = document.querySelector(".elseText");
	let confirm = document.querySelector(".confirm");
	let levImg = document.querySelector(".levImg");
	let classNum = '';
//	let classify = document.querySelector(".classify");
	let deNum = '1';
	let photo = "";
	
	renderForm()
	
	
	
	function renderForm(){
	  layui.use('form', function(){
	   var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
	   form.render();
	  });
	}
	
//	function rank(){
//		DBHelper.Get("Admin_Xn_RankCategory",function(data){
//			console.log(data)
//		},function(data){
//			console.log(data)
//		})
//	}
	
//	rank()
	
	$('input:radio[value='+0+']').attr('checked', 'true');
	
	console.log("123",$("input[type='radio']:checked").val())
	
	layui.use(['jquery', 'mockjs', 'table', 'sidebar', 'form','route'], function () {
		var $ = layui.jquery,
      layer = layui.layer,
      table = layui.table,
      sidebar = layui.sidebar,
      route = layui.route,
      params = route.params(),
      form = layui.form;
      
      DBHelper.GetA("Admin_Xn_RankCategory",function(data){
//    	console.log(data)
      	data.forEach((el,index)=>{
    		  	let op = document.createElement('option');
    		  	
    		  	op.value = el.Id;
    		  	op.innerText = el.Name;
    		  	classify.appendChild(op)
//		    		  	console.log(op.id)
						form.render();
						
    		  })
      },function(data){
      	console.log(data)
      })
      
      
      
//    $(".isDefault").find("option[value='"+obj.CategoryId+"']").prop("selected",true);
      
      var _obj ={
					  "Guid": "00000000-0000-0000-0000-000000000000",
					  "Name": level.value,
					  "IsDefault": "",
					  "CategoryId": 1,
					  "Score": score.value,
					  "Number": "",
					  "Images": "",
					  "CreateUser": "",
					  "CreateTime": "",
					  "ModifyUser": "",
					  "ModifyTime": ""
				  };

					function _confirm (){
						_obj.Score = score.value;
//						_obj.Number = num.value;
						_obj.Name = level.value;
						
					 	_obj.Images = photo;
					 	_obj.IsDefault = $("input[type='radio']:checked").val();
					 	console.log(999,_obj.IsDefault)
					 	_obj.CategoryId = $(".classify option:selected").val();
					 
					 
						if(params == null) {
							_obj.CreateUser = localStorage.getItem("user");
							_obj.CreateTime = getNowFormatDate();
							
							 
//							_obj.CreateTime = new Date();
							DBHelper.Post("Admin_Xn_Rank", _obj, function(data) {
								console.log(data)
								let index = layer.alert("操作成功",function(){
				  				window.location.href="#/layui/memberLev";
				  				layer.close(index)
				  			})
								
							}, function(data) {
								console.log(data)
							});
						} else {
//							_obj.ModifyTime = new Date();
							_obj.ModifyUser = localStorage.getItem("user");
							_obj.ModifyTime = getNowFormatDate();
							DBHelper.Put("Admin_Xn_Rank?id=" + params.id, _obj, function(data) {
								let index = layer.alert("操作成功",function(){
				  				window.location.href="#/layui/memberLev";
				  				layer.close(index)
				  			})
								
							}, function(data) {
								console.log(data)
								let index = layer.alert("每个等级都必须要有一个默认等级",function(){
									location.href = window.location.href;
									layer.close(index)
								})
							})
						}
					}

			if(params) {
				console.log("params.id",params)
				DBHelper.Get("Admin_Xn_Rank?id="+params.id,function(data){
				      	_obj = data;
				      	console.log(data)
//				      	num.value = data.Number;
				      	level.value = data.Name; 
				      	score.value = data.Score;
				      	photo = data.Images;
				      	$('input:radio[value='+data.IsDefault+']').attr('checked', 'true');
								
				      	console.log($('input:radio').val())
				      	if(data.Images){
				      		levImg.setAttribute("src",data.Images)
				      	}else{
				      		levImg.setAttribute("src","img/addImg.png")
				      	}
				      	
				      	console.log("data.IsDefault",data.IsDefault)
				      	$(".classify").find("option[value='"+data.CategoryId+"']").prop("selected",true);
				      	
				      	renderForm()
			  },function(data){
			  	console.log(data)
			  	
			  })
			}else{
//				$('input:radio[value="1"]').attr('checked', 'true');
			}
      
//   confirm.addEventListener("click",_confirm);
			form.on('submit(queding)',_confirm)
      
     
      
      
      
    })
	
	function uploadPhoto2(file) {
	    console.log(file.files[0])
	
	    var data = new FormData();
	    data.append("file", file.files[0]);
	    
	
	    $.ajax({
	      url:DBHelper.host+'/api/ImgUpload?MemLoginId=admit&ImagesType=',
	      type: 'POST',
	      dataType: 'JSON',
	      data: data,
	      cache: false,
	      processData: false,
	      contentType: false,
	      beforeSend: function(request) {
	          request.setRequestHeader("Authorization", 'bearer ' + DBHelper.getToken());
	          loadingImg()
	      },
				success:function(res){
//					let fhImg = document.querySelector(".fhImg");
//					console.log(res.Url)
//					fhImg.setAttribute("src",res.Url)
//					photo = res.Url;
					console.log("res",res)
					levImg.setAttribute("src",res.Url)
					photo = res.Url;
					$('.loading').remove()
				},
				error:function(res){
					$('.loading').remove()
				}
	    })
	 }	
</script>


